package com.example.homework07.service;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.homework07.R;

public class bindservice extends Fragment {
    public bindservice(){
        super(R.layout.bindservice);
    }

    private MyConn myConn;
    public class MyConn implements ServiceConnection {
        private MusicService2.MyBinder binder = null;
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            binder = (MusicService2.MyBinder)service;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    }
    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        getActivity().startService(new Intent(getContext(), MusicService2.class));
        if(myConn == null){
            myConn = new MyConn();
            getActivity().bindService(
                    new Intent(getActivity(),MusicService2.class),myConn,0);
        }
        getView().findViewById(R.id.bdbf).setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(myConn.binder == null)
                            return;
                        myConn.binder.play();
                    }
                }
        );
        getView().findViewById(R.id.bdxys).setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(myConn.binder == null)
                            return;
                        myConn.binder.next();
                    }
                }
        );
        getView().findViewById(R.id.bdtz).setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(myConn.binder == null)
                            return;
                        myConn.binder.pause();
                    }
                }
        );
    }
}
